package com.genesis.yunnanji.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.activity.WebActivity;
import com.genesis.yunnanji.adapter.NewsHomeAdapter2;
import com.genesis.yunnanji.bean.HomePageListBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.GenesisUtils;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.genesis.yunnanji.view.CustomLoadMoreView;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONObject;
import org.xutils.http.RequestParams;

import java.util.List;

/**
 * Created by Administrator on 2018/4/17.
 */

public class Subject_Video_Visit extends Fragment {
    private View view;
    private RecyclerView recyclerView;
    private TwinklingRefreshLayout refreshLayout;
    private GenesisUtils genesisUtils;
    private int page = 1;
    private Intent intent;
    private String connectUrl;

    public static Subject_Video_Visit newInstance(int param) {
        Subject_Video_Visit obj = new Subject_Video_Visit();
        Bundle bundle = new Bundle();
        bundle.putInt("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_videosubjectvisit, null);
            if (getArguments() != null) {
                if (getArguments().getInt("args1") == 1) {
                    connectUrl = GenesisApiConfig.VISIT;
                } else if (getArguments().getInt("args1") == 2) {
                    connectUrl = GenesisApiConfig.VIDEO;
                } else if (getArguments().getInt("args1") == 3) {
                    connectUrl = GenesisApiConfig.SUBJECT;
                }
            }
            recyclerView = (RecyclerView) view.findViewById(R.id.rv_vsv);
            refreshLayout = (TwinklingRefreshLayout) view.findViewById(R.id.bga_vsv_refresh);
            genesisUtils = GenesisUtils.getIntance();
            getNewsData(refreshLayout);
            initView();
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }

        return view;
    }

    protected void initView() {

        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                page = 1;
                if (arrayNews != null) {
                    arrayNews.removeAll(arrayNews);
                }
                getNewsData(refreshLayout);
            }
        });


    }

    HomePageListBean homePageListBean;
    NewsHomeAdapter2 newsHomeAdapter2;
    List<HomePageListBean.ResultBean.ListBean> arrayNews;

    private void getNewsData(final TwinklingRefreshLayout refreshLayout) {
        final RequestParams params = new RequestParams(GenesisApiConfig.HOST + connectUrl);
        params.addBodyParameter("p", page + "");
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Log.e("data", jsonObject.toString());
                Gson gson = new Gson();
                homePageListBean = gson.fromJson(jsonObject.toString(), HomePageListBean.class);

                if (page == 1) {
                    arrayNews = homePageListBean.getResult().getList();
                    newsHomeAdapter2 = new NewsHomeAdapter2(arrayNews, getActivity(), false);
                    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                    recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).color(getResources().getColor(R.color.divider)).build());
                    recyclerView.setAdapter(newsHomeAdapter2);
                    newsHomeAdapter2.setLoadMoreView(new CustomLoadMoreView());
                    newsHomeAdapter2.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                        @Override
                        public void onLoadMoreRequested() {
                            page += 1;
                            getNewsData(refreshLayout);
                        }
                    });
                    newsHomeAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            intent = new Intent(getActivity(), WebActivity.class);
                            if (genesisUtils.isLogin()) {
                                intent.putExtra("url", arrayNews.get(position).getTaget_link() + GenesisApiConfig.FROM_APP + GenesisApiConfig.APP_TOKEN + genesisUtils.getAppToken());
                            } else {
                                intent.putExtra("url", arrayNews.get(position).getTaget_link() + GenesisApiConfig.FROM_APP);
                            }
                            startActivity(intent);
                        }
                    });
                } else {
                    if (homePageListBean.getStatus() == 200) {
                        newsHomeAdapter2.addData(homePageListBean.getResult().getList());
                        newsHomeAdapter2.loadMoreComplete();
                    } else {
                        newsHomeAdapter2.loadMoreEnd();
                    }

                }
                Log.e("size", arrayNews.size() + "");


            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
                if (refreshLayout != null) {
                    refreshLayout.finishRefreshing();

                }
            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }


}
